package com.kun.myproject;

import android.util.Log;

import com.kun.service.BluetoothLeService;
import com.kun.utils.DataUtil;

/**
 * Created by lirongkun on 16/8/22.
 */
public class MainPresent {
    private MainView view;
    private byte[] bytes = {(byte)0xfa,0x01,0x00,0x02,(byte)0xfd};
    private boolean opcode3first = true;
    private StringBuffer opcode3data;
    private int count3 = 0, counting3 = 0;//opcode=3时计数判断

    private boolean opcode4first = true;
    private StringBuffer opcode4data;
    private int count4 = 0, counting4 = 0;//opcode=4时计数判断
    public MainPresent(MainView view){
        this.view = view;
        opcode3data = new StringBuffer();
        opcode4data = new StringBuffer();
    }

    /**
     *
     * @param data 解析对象
     * @param opcode 解析对应opcode
     */
    public void analysis(String data,int opcode){
        switch (opcode){
            case 0:
                byte[] bytes = DataUtil.getBytesByString(data.substring(10,data.length()-2));
                view.DataChange(String.valueOf(byte2char(bytes)),opcode);
                break;
            case 1:
                if ("FA02000100FD".equals(data)){
                    view.DataChange("复位成功",opcode);
                }else {
                    view.DataChange("复位失败",opcode);
                }
                break;
            case 2:
                if (data.length()!=24) return;
                view.DataChange(data.substring(9,data.length()-2),opcode);
                break;
            case 3:
                if (opcode3first){
                    if (data.length()<40){
                        Log.i("aaa","length<40");
                        view.DataChange(data.substring(10,data.length()-2),opcode);
                    }else {
                        opcode3data.delete(0,opcode3data.length());
                        opcode3first = false;
                        byte[] mybyte = DataUtil.getBytesByString(data);
                        count3 = (mybyte[1] + mybyte[2]*256 + 4)/20;
                        opcode3data = opcode3data.append(data.substring(10,data.length()));
                        Log.i("aaa","count3:"+count3);
                    }
                }else {
                    counting3++;
                    Log.i("aaa","counting3:"+counting3);
                    if (counting3 == count3){
                        opcode3first = true;
                        opcode3data.append(data.substring(0,data.length()-2));
                        view.DataChange(opcode3data.toString(),opcode);
                        counting3 = 0;
                        Log.i("aaa","opcode3data:"+opcode3data);
                    }else {
                        opcode3data.append(data);
                    }
                }
                break;
            case 4:
                if (opcode4first){
                    if (data.length()<40){
                        view.DataChange(data.substring(10,data.length()-2),opcode);
                    }else {
                        opcode4data.delete(0,opcode4data.length());
                        opcode4first = false;
                        byte[] mybyte = DataUtil.getBytesByString(data);
                        count4 = (mybyte[1] + mybyte[2]*256 + 4)/20;
                        opcode4data = opcode4data.append(data.substring(10,data.length()));
                    }
                }else {
                    counting4++;
                    Log.i("aaa","count4:"+count4);
                    if (counting4 == count4){
                        opcode4first = true;
                        opcode4data.append(data.substring(0,data.length()-2));
                        view.DataChange(opcode4data.toString(),opcode);
                        counting4 = 0;
                    }else {
                        opcode4data.append(data);
                    }
                }
                break;
            case 5:
                if ("FA0200050001".equals(data)){
                    view.DataChange("关闭成功",opcode);
                }else {
                    view.DataChange("关闭失败",opcode);
                }
                break;
            case 6:
                if (data.length()!=14) return;
                view.DataChange(data.substring(10,12),opcode);
                break;
            default:
                break;
        }
    }

    public void write2ble(final String data, final int opcode,final BluetoothLeService mBluetoothLeService){
        if(opcode != 3){
            bytes[3] = (byte)opcode;
            bytes[4] = (byte)(bytes[0] + bytes[1] + bytes[2] +bytes[3]);
            mBluetoothLeService.writeStringToGatt(bytes,opcode);
            view.SendData(DataUtil.getStringByBytes(bytes));
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                short sum = 0;
                byte[] new_byte = DataUtil.getBytesByString(data);
                int len = new_byte.length;
                for(int i = 0;i<len;i++){
                    sum += new_byte[i];
                }
                int len_l = (len+1)%256;//低位
                int len_h = (len+1)/256;//高位
                byte checksum = (byte)(sum+len_h+len_l+3+0xfa);
                Log.i("aaa","checksum:"+sum);
                opcode3first = true;
                if (len<16){
                    byte[] cmd = new byte[len+5];
                    for (int j = 0;j<len;j++){
                        cmd[4+j] = new_byte[j];
                    }
                    cmd[0] = (byte)0xfa;
                    cmd[1] = (byte)len_l;
                    cmd[2] = (byte)len_h;
                    cmd[3] = 0x03;
                    cmd[len + 4] = checksum;
                    mBluetoothLeService.writeStringToGatt(cmd,opcode);
                    view.SendData(DataUtil.getStringByBytes(cmd));
                    Log.i("aaa","sum:"+sum+"  length:"+new_byte.length +"byte:"+DataUtil.getStringByBytes(cmd));
                }else {
                    int count = (len+24)/20;
                    for (int a = 0;a<count;a++){
                        if (a == 0){
                            byte[] cmd = new byte[20];
                            cmd[0] = (byte)0xfa;
                            cmd[1] = (byte)len_l;
                            cmd[2] = (byte)len_h;
                            cmd[3] = 0x03;
                            for (int j = 0;j<16;j++){
                                cmd[4+j] = new_byte[j];
                            }
                            Log.i("aaa",a+"+byte:"+DataUtil.getStringByBytes(cmd));
                            mBluetoothLeService.writeStringToGatt(cmd,opcode);
                            view.SendData(DataUtil.getStringByBytes(cmd));
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else if(a == count-1){
                            int locate = (len+5)%20;
                            byte[] cmd = new byte[locate];
                            for (int j = 0;j<locate-1;j++){
                                cmd[j] = new_byte[len+1-locate+j];
                            }
                            cmd[locate - 1] = checksum;
                            mBluetoothLeService.writeStringToGatt(cmd,opcode);
                            view.SendData(DataUtil.getStringByBytes(cmd));
                            Log.i("aaa",a+"+byte:"+DataUtil.getStringByBytes(cmd));
                        }else {
                            byte[] cmd = new byte[20];
                            for (int j = 0;j<20;j++){
                                cmd[j] = new_byte[16+(a-1)*20+j];
                            }
                            mBluetoothLeService.writeStringToGatt(cmd,opcode);
                            view.SendData(DataUtil.getStringByBytes(cmd));
                            Log.i("aaa",a+"+byte:"+DataUtil.getStringByBytes(cmd));
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

        }).start();
    }
    //byte[]转char[]
    private char[] byte2char(byte[] bytes){
        char[] chars= new char[bytes.length];
        for (int i = 0;i < bytes.length;i++){
            chars[i] = (char)bytes[i];
        }
        return chars;
    }
}
